package com.jswzc.framework.util;


import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;

class myAuthenticator extends Authenticator
{
	String userName;
	String userPass;

	public myAuthenticator()
	{
	}

	public myAuthenticator(String userName, String userPass)
	{
		this.userName = userName;
		this.userPass = userPass;
	}

	protected PasswordAuthentication getPasswordAuthentication()
	{
		return new PasswordAuthentication(userName, userPass);
	}
}

public class MailUtil
{
	/** 网易163邮箱 */
	public static final String email163 = "smtp.163.com";

	/** 网易126邮箱 */
	public static final String email126 = "SMTP.126.COM";

	/** QQ邮箱 */
	public static final String emailQQ = "SMTP.QQ.COM";

	/** 新浪邮箱 */
	public static final String emailSina = "SMTP.SINA.CN";

	/** 新浪免费企业邮箱 */
	public static final String emailQYSina = "smtp.exmail.sina.com";

	/** 189邮箱 */
	public static final String email189 = "SMTP.189.CN";

	/** souhu邮箱 */
	public static final String emailSouhu = "SMTP.SOHU.COM";

	/**
	 * 发送邮件
	 * 
	 * @param mailServerHost
	 *            邮件服务器地址
	 * @param mailServerPort
	 *            邮件服务器端口
	 * @param validate
	 *            是否要求身份验证
	 * @param fromAddress
	 *            发送邮件地址
	 * @param toAddress
	 *            接收邮件地址
	 * @param subject
	 *            邮件主题
	 * @param content
	 *            邮件内容
	 * @param isHTML
	 *            是否是html格式邮件
	 * @param isSSL
	 *            邮件服务器是否需要安全连接(SSL)
	 * @return true:发送成功;false:发送失败
	 */
	public static boolean sendMail(String mailServerHost, String mailServerPort, boolean validate, String fromAddress, String userPass, String toAddress, String subject, String content,
			boolean isHTML, boolean isSSL)
	{
		Properties p = new Properties();
		p.put("mail.smtp.host", mailServerHost);
		p.put("mail.smtp.port", mailServerPort);
		p.put("mail.smtp.auth", validate ? "true" : "false");
		if (isSSL)
		{
			p.put("mail.smtp.starttls.enable", "true");
			p.put("mail.smtp.socketFactory.fallback", "false");
			p.put("mail.smtp.socketFactory.port", mailServerPort);
		}
		Authenticator auth = null;
		if (validate)
		{
			auth = new myAuthenticator(fromAddress, userPass);
		}

		try
		{
			Session session = Session.getDefaultInstance(p, auth);
			Message message = new MimeMessage(session);
			Address from = new InternetAddress(fromAddress);
			Address to = new InternetAddress(toAddress);
			message.setFrom(from);
			message.setRecipient(Message.RecipientType.TO, to);
			message.setSubject(subject);
			message.setSentDate(new Date());
			if (isHTML)
			{
				Multipart m = new MimeMultipart();
				BodyPart bp = new MimeBodyPart();
				bp.setContent(content, "text/html; charset=utf-8");
				m.addBodyPart(bp);
				message.setContent(m);
			}
			else
				message.setText(content);
			Transport tran = session.getTransport("smtp");
			tran.connect(mailServerHost, fromAddress, userPass);
			tran.sendMessage(message, message.getAllRecipients());
			return true;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}

	public static boolean validateAccount(String userName, String userPass)
	{
		Properties p = new Properties();
		p.put("mail.smtp.host", MailUtil.email163);
		p.put("mail.smtp.port", -1);
		p.put("mail.smtp.auth", "true");
		p.put("mail.smtp.starttls.enable", "true");
		p.put("mail.smtp.socketFactory.fallback", "false");
		p.put("mail.smtp.socketFactory.port", -1);
		Authenticator auth = new myAuthenticator(userName, userPass);
		try
		{
			Session session = Session.getDefaultInstance(p, auth);
			System.out.println(session.toString());
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}

		return false;
	}

	public static void main(String[] args)
	{
		System.out.println("hello");
		
		boolean f=sendMail(MailUtil.email163, "25", true, "jian_zhan_xuan_zhi@163.com", "tzdxjzxz", "417823405@qq.com", "邮件发送测试", "邮件正文", false, false);

		if (f)
		{
			System.out.println("邮件发送成功!");
		} else
		{
			System.out.println("邮件发送失败!");
		}
	}

}


